﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.ComponentModel;

namespace XtremBiking.Web.Atributes
{
    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
    public sealed class IsDifferent : ValidationAttribute
    {
        public IsDifferent(string value1, string value2)
        {
         Value1 = value1;
         Value2 = value2;
        }
 
        public string Value1 { get; private set; }
        public string Value2 { get; private set; }
 
        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString);
        }
 
        public override bool IsValid(object value)
        {
             var properties = TypeDescriptor.GetProperties(value);
             var value1 = properties.Find(Value1, true).GetValue(value);
             var value2 = properties.Find(Value2, true).GetValue(value);

             return !Equals(value1, value2);
             
        }
    }
}
